package br.com.vti.sisp_sicrim.antigo.controller.programa;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Relatorioparam;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;

@Resource
@Path("/antigo/relparam")
public class Relparam {
	Result result;
	Sessao sessao;
	Relatorioparam relatorioparam;
	Relparam relparamAnterior;

	public Relparam(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	private Relatorioparam preMapas() {

		relatorioparam = new Relatorioparam();

		return relatorioparam;
	}

	@Post
	public void executar(Relatorioparam relatorioparam) {
		try {
			main(relatorioparam);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, "");
		}
	}

	private void main(Relatorioparam prelatorioparam) throws Exception {
		if (prelatorioparam == null) {
			preMapas();
			relatorioparam.setNavegar("../relparam/executar");
			relatorioparam.setTitulo((String) sessao.getAtributo("titulo"));
			result.include("relatorioparam", relatorioparam);
			sessao.setAtributo("RELPARAM", this);
			relatorioparam.enviarTela(true, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(prelatorioparam);
		}

		relatorioparam.setNavegar("../relparam/executar");
		result.include("relatorioparam", relatorioparam);
		sessao.setAtributo("RELPARAM", this);
		relatorioparam.enviarTela(true, result, getClass().getSimpleName().toUpperCase());

	}

	@Post
	@Get
	public void menu() {
		String classe = (String) sessao.getAtributo("classe");
		if (classe != null) {
			new FuncoesGerais().executarModulo(classe, "", result, sessao);
		} else {
			Scrp1000 scrp1000 = new Scrp1000(result, sessao);
			scrp1000.executar(null);

		}
	}

	@Post
	@Get
	public InputStreamDownload relatorio() {

		return (InputStreamDownload) sessao.getAtributo("relatorio");
	}

	private void restaurarEstados(Relatorioparam prelatorioparam) {
		this.relparamAnterior = (Relparam) sessao.getAtributo("RELPARAM");
		if (prelatorioparam != null) {
			relatorioparam = prelatorioparam;
		} else {
			relatorioparam = this.relparamAnterior.relatorioparam;
		}
		relatorioparam.setSessao(sessao);

	}
}
